/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1602.czq.dao.impl;

import edu.fjut.se1602.czq.dao.PetOwnerDao;
import edu.fjut.se1602.czq.entity.Pet;
import edu.fjut.se1602.czq.entity.PetOwner;
import edu.fjut.se1602.czq.entity.PetStore;
import edu.fjut.se1602.czq.service.impl.PetOwnerServiceImpl;
import edu.fjut.se1602.czq.service.impl.PetServiceImpl;
import edu.fjut.se1602.czq.service.impl.PetStoreServiceImpl;
import edu.fjut.se1602.czq.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author asus
 */
public class PetOwnerDaoImpl implements PetOwnerDao {

    @Override
    public int Login(String userName, String userPassword) {
        int r = 0;
        //PetOwner petowner = new PetOwner();
        try {
            Connection con = DBUtil.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from owner where name=? and password=? ");
            pst.setString(1, userName);
            pst.setString(2, userPassword);
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                System.out.println("------恭喜你登陆成功！------");
                System.out.println("------您的基本信息：------");
                System.out.println("名字：" + userName + "\n" + "元宝数：" + rs.getDouble("money"));
                System.out.println("登录成功，您可以购买和卖出宠物，购买宠物请输入1，卖出请输入2" + "\n" + "1:购买宠物" + "\n" + "2:卖出宠物");
                Scanner sc = new Scanner(System.in);
                int dealType = sc.nextInt();
                if (dealType == 1) {
                    ownerBuy(userName);
                    System.out.println("购买成功！");
                } else if (dealType == 2) {
                    ownerSell(userName);
                }
            } else {
                System.out.println("登录失败！");
            }
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public List<PetOwner> getAllOwner() {
        List<PetOwner> arraylist = new ArrayList<PetOwner>();
        try {
            Connection con = DBUtil.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from owner");
            while (rs.next()) {
                PetOwner petowner = new PetOwner();
                petowner.setId(rs.getInt("ID"));
                petowner.setName(rs.getString("name"));
                petowner.setPassword(rs.getString("password"));
                petowner.setMoney(rs.getDouble("money"));
                arraylist.add(petowner);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return arraylist;
    }

    @Override
    public int ownerBuy(String ownerName) {
        PetServiceImpl psi = new PetServiceImpl();
        PetOwnerServiceImpl posi = new PetOwnerServiceImpl();
        List<Pet> rePets = new ArrayList();
        List<PetOwner> owners = new ArrayList();
        rePets = psi.getPets();
        owners = posi.getOwners();
        System.out.println("------以下是库存宠物------");
        for (Pet repet : rePets) {
            System.out.println("序号：" + repet.getID() + "   我的名字叫" + repet.getName() + "   我是：" + repet.getTypeName() + "   要购买我要花：" + repet.getPrice() + "个元宝");
        }
        System.out.println("------请选择要购买哪一个宠物，并输入选择项的序号------");
        Scanner sc = new Scanner(System.in);
        int buyNo = sc.nextInt();//购买宠物的序号
        int buyId = 0;//买家id
        double buyMoney=0;
        for (PetOwner owner : owners) {
            if (owner.getName().equals(ownerName)) {
                buyId = owner.getId();
            }
        }
        for (Pet repet : rePets) {
            if (buyNo == repet.getID()) {
                repet.setOwner_id(buyId);
                buyMoney=repet.getPrice();
            }
        }
        PetOwner owner =new PetOwner();
        Pet pet =new Pet();
        owner.setMoney(owner.getMoney()-buyMoney);
        try {
            Connection con = DBUtil.getConnection();
            PreparedStatement pst1 = con.prepareStatement("update pet set OWNER_ID=? where ID=?");
            PreparedStatement pst2 = con.prepareStatement("update owner set MONEY=? where ID=?");
            pst1.setInt(1, buyId);
            pst1.setInt(2, buyNo);
            pst2.setDouble(1, owner.getMoney());
            pst2.setInt(2, buyId);
            pst1.executeUpdate();
            pst2.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return buyId;
    }

    @Override
    public int ownerSell(String ownerName) {
        int temp = 1;
        PetServiceImpl psi = new PetServiceImpl();
        PetStoreServiceImpl pssi = new PetStoreServiceImpl();
        PetOwnerServiceImpl posi = new PetOwnerServiceImpl();
        List<Pet> rePets = new ArrayList();
        List<PetOwner> owners = new ArrayList();
        List<PetStore> stores = new ArrayList();
        rePets = psi.getPets();
        owners = posi.getOwners();
        stores = pssi.getStores();
        int buyID=0;
        System.out.println("------我的宠物列表------");
        for (PetOwner owner : owners) {
            buyID=owner.getId();
            for (Pet pet : rePets) {
                if (owner.getId() == pet.getOwner_id()) {
                    System.out.println("序号：" + temp + "   我的名字叫" + pet.getName() + "   我是：" + pet.getTypeName());
                    temp++;
                }
            }
        }
        System.out.println("------请选择要出售的宠物序号------");
        Scanner sc = new Scanner(System.in);
        int sellNo = sc.nextInt();//出售宠物的序号
        
        double sellMoney=0;
        System.out.println("---你要卖出的宠物信息如下---");
        for (Pet repet : rePets) {
            for (PetOwner owner : owners) {
                if (repet.getOwner_id() == owner.getId()) {
                    System.out.println("   宠物名字叫" + repet.getName() + "   宠物类别是：" + repet.getTypeName());
                    sellMoney=repet.getPrice();
                }
            }
            System.out.println("请确认是否卖出，y代表卖出，n代表不卖");
            String yn = sc.next();
            if (yn.equals("y")) {
                System.out.println("---下面是现有宠物商店，请选择你要卖给买家序号---");
                for (PetStore store : stores) {
                    System.out.println("序号：" + store.getID() + "  宠物商店的名字：" + store.getName());
                }
                int storeNo = sc.nextInt();
                break;
            } else if (yn.equals("n")) {
                System.out.println("交易取消");
            }
        }
        System.out.println("出售成功！");
        PetOwner owner =new PetOwner();
        Pet pet =new Pet();
        owner.setMoney(owner.getMoney()+sellMoney);
        try {
            Connection con = DBUtil.getConnection();
            PreparedStatement pst = con.prepareStatement("update owner set MONEY=? where ID=?");
            pst.setDouble(1, owner.getMoney());
            pst.setInt(2, buyID);
            pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

}
